package cn.dinkevin.xui.util;


import android.util.Log;

import java.util.ArrayList;
import java.util.List;

/**
 * 调试打印输出工具类
 * @author chengpengfei
 */
public class LogUtil {

	private static boolean g_enable = true;	 		 			// 调试总输出开关
	public static final String TAG = "XUI";
	public static final String TAG_D = "[" + TAG + "_D]";
	public static final String TAG_E = "[" + TAG + "_E]";

	/**
	 * 获取指定对象的 TAG
	 * @param object
	 * @return
     */
	public static String getTag(Object object){
		return object.getClass().getSimpleName();
	}

	/**
	 * 设置调用输出开关
	 * @param enable true -> 开启打印调试信息;false -> 关闭打印调试信息
	 */
	public static void setEnable(boolean enable){
		g_enable = enable;
	}
	
	/**
	 * 获取当前打印输出是否开启
	 * @return true -> 开启打印调试信息;false -> 关闭打印调试信息
	 */
	public static boolean isEnable(){
		return g_enable;
	}

	/**
	 * 将对象数组转换到字符串
	 * @param params
	 * @return
     */
	private static StringBuffer _convertToString(Object... params){
		StringBuffer buffer = new StringBuffer();
		for(Object obj : params)
		{
			buffer.append(obj);
			buffer.append(" ");
		}
		return buffer;
	}

	/**
	 * 打印  debug 信息
	 * @param params
	 * @return 已经打印的字符串
	 */
	@Deprecated
	public static String d(Object... params)
	{
		if(!g_enable) return null;
		if(params == null) return null;
		StringBuffer buffer = new StringBuffer();
		buffer.append(TAG_D);
		String content = _convertToString(params).toString().replaceAll("\n","\n" + TAG_D);
		buffer.append(content);
		String message = buffer.toString();
		System.out.println(message);
		return message;
	}
	
	/**
	 * 打印异 debug 信息
	 * @param params
	 * @return 已打印的字符串
	 */
	@Deprecated
	public static String e(Object... params)
	{
		if(!g_enable) return null;
		if(params == null) return null;
		StringBuffer buffer = new StringBuffer();
		buffer.append(TAG_E);
		String content = _convertToString(params).toString().replaceAll("\n","\n" + TAG_E);
		buffer.append(content);
		String message = buffer.toString();
		System.out.println(message);
		return message;
	}

	public static List<String> mLogCache = new ArrayList<>();

	/**
	 * 格式化打印
	 * @param format
	 * @param params
	 */
	public static void format_d(String format, Object... params){

		if(!g_enable) return;
		Log.d(TAG,String.format(format,params));
		mLogCache.add(String.format(format,params));
	}


	/**
	 * 格式化打印
	 * @param format
	 * @param params
	 */
	public static void format_e(String format, Object... params){

		if(!g_enable) return;
		Log.e(TAG,String.format(format,params));
		mLogCache.add(String.format(format,params));
	}
}
